<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  {
    let arr = [1, 2, 3, 4, 5, 6];
    arr.reduce((pre, value, index, array) => {
      // pre是函数的返回值
      console.log(pre, value);
      return 30;
    });

    arr.reduce((pre, value, index, array) => {
      console.log(pre, value);
      return 30;
    }, 0); //设置初始值

  }; {
    let arrs = [2, 3, 3, 1, 1, 3, 2];
    // 统计一下数组中出现了多少个3
    let arrayCount = (arr, item) => {
      return arr.reduce((pre, value, index, array) => {
        // 先判断item和value是否相等，若相等则返回1，后再和pre相加
        console.log(value);
        pre += item == value ? 1 : 0;
        console.log(`=== ${pre} ===`);
        return pre;
      }, 0); //设置初始值
    };

    let i = arrayCount(arrs, 3);
    console.log(i);
  }; {
    // 获取元素的最大值的方法
    let arr = [1, 2, 100, 100000, 13, 2334];
    let arrayMax = (arr) => {
      return arr.reduce((pre, value, index, array) => {
        return pre > value ? pre : value; //若pre大于value，则返回pre
      }, 0);
    };

    let j = arrayMax(arr);
    console.log(j);
  }
</script>

</html>